<?php
/**
 * 收货地址控制器
 */
class ReceivAddrController extends CommonController {

	private $model;

	
	public function __init() {
		$this -> model = K('ReceiveAddr');
	}
	
	/**
	 * 删除一个收货地址
	 */
	 public function delReceivAddr(){
	 	$reid = Q('get.reid');
		if($this->model->where("reid={$reid}")->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	 }
	
	
	
	
	
	/**
	 * 修改相应的收货地址
	 */
	 public function editReceivAddr(){
	 	$reid = Q('get.reid');
		$city = K('city');
		if(IS_POST){
			$_POST['address'] = $_POST['address_a'];
			$_POST['re_addarea'] = $_POST['address']; 
			unset($_POST['address_a']);
			unset($_POST['address']);
			
			if($this->model->editAddr()){
				$this->success('修改成功');
			}else{
				$this->error($this->model->error);
			}
		}
		
		//获得到收货地址的那条信息
		$addr = $this->model->where("reid={$reid}")->find();
		//将收货地址分配 到页面
		$this->assign('readdr',$addr);
		//分类省份ID
		$address = explode(',', $addr['re_addarea']);
		$this->assign('addressIds',$address);		
		//省份
		$this->assign('prov',$city->getAllProvince());
		//城市
		$this->assign('citys',$city->getProvCity($address[0]));
		//县
		$this->assign('dists',$city->getDist($address[1]));
		$this->display();
	 }
	
	

	/**
	 * 获得相应的UID的收货地址
	 */
	public function receivAddr() {
		$uid = Q('get.uid');
		if(IS_POST){
			$_POST['re_addarea'] = $_POST['address'];
			if($this->model->addAddr()){
				$this->success('添加收货地址成功');
			}else{
				$this->error($this->model->error);
			}
		}
		$addr = $this -> model -> getUserRa($uid);
		$this -> assign('addrs', $addr);
		$this -> display();
	}

	/**
	 * 给指定用户添加相应的收货地址
	 */
	public function addReceiveAddr() {
		$uid = Q('get.uid');
		//实例化城市模型
		$city = K('City');
		//查询到所有的省份
		$this -> assign('province', $city -> getAllProvince());
		$this -> display();
	}

}
